package com.loser.utils;

import cn.hutool.core.convert.Convert;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据工具类
 *
 * @author alan
 * @date 2022/05/17 16:22
 */
public class NumberUtils {

    public static final int MAX = 100000000;
    public static final int MIN = 10000;
    private static final String SPLIT = ".";
    private static final String SPACE = "";
    private static final String WAN = "万";
    private static final String YI = "亿";
    private static final String UNDER = "_";

    public static String splitNum(long num) {
        String s = Convert.toStr(num);
        char[] chars = s.toCharArray();
        int count = 0;
        String x = "";
        int a = chars.length - 1;
        for (int i = a; i >= 0; i--) {
            count++;
            x = chars[i] + x;
            if (count == 3 && i != 0) {
                x = "," + x;
                count = 0;
            }
        }
        return x;
    }

    public static Object sortCpMember(Object member, Object otherMember) {

        ArrayList<String> list = new ArrayList<>();
        list.add(Convert.toStr(member));
        list.add(Convert.toStr(otherMember));
        return list.stream().sorted().collect(Collectors.joining(UNDER));

    }

    public static Object buildRoomMember(String module, Object otherMember) {
        return module + UNDER + otherMember;
    }

    public static List<Long> splitCpMember(Object sortCpMember) {
        return Arrays.stream(Convert.toStr(sortCpMember).split(UNDER)).map(Convert::toLong).collect(Collectors.toList());
    }

    public static String unit(long num) {
        if (num < MIN) {
            return num + SPACE;
        } else if (num < MAX) {
            return format(num, MIN, WAN);
        } else {
            return format(num, MAX, YI);
        }
    }

    private static String format(double num, int min, String wan) {
        double n = num / min;
        String s = String.format("%.2f", n);
        if (s.indexOf(SPLIT) > 0) {
            // 去掉多余的0
            s = s.replaceAll("0+?$", SPACE);
            // 如最后一位是.则去掉
            s = s.replaceAll("[.]$", SPACE);
        }
        return s + wan;
    }

    /**
     * 中文数字
     */
    private static final String[] CN_NUM = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};

    /**
     * 中文数字单位
     */
    private static final String[] CN_UNIT = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};

    /**
     * 特殊字符：负
     */
    private static final String CN_NEGATIVE = "负";

    /**
     * 特殊字符：点
     */
    private static final String CN_POINT = "点";


    /**
     * int 转 中文数字
     * 支持到int最大值
     */
    public static String int2chineseNum(int intNum) {
        StringBuilder sb = new StringBuilder();
        boolean isNegative = false;
        if (intNum < 0) {
            isNegative = true;
            intNum *= -1;
        }
        int count = 0;
        while (intNum > 0) {
            sb.insert(0, CN_NUM[intNum % 10] + CN_UNIT[count]);
            intNum = intNum / 10;
            count++;
        }

        if (isNegative) {
            sb.insert(0, CN_NEGATIVE);
        }


        return sb.toString().replaceAll("零[千百十]", "零").replaceAll("零+万", "万")
                .replaceAll("零+亿", "亿").replaceAll("亿万", "亿零")
                .replaceAll("零+", "零").replaceAll("零$", "");
    }

    /**
     * bigDecimal 转 中文数字
     * 整数部分只支持到int的最大值
     */
    public static String bigDecimal2chineseNum(BigDecimal bigDecimalNum) {
        if (bigDecimalNum == null) {
            return CN_NUM[0];
        }

        StringBuilder sb = new StringBuilder();

        //将小数点后面的零给去除
        String numStr = bigDecimalNum.abs().stripTrailingZeros().toPlainString();

        String[] split = numStr.split("\\.");
        String integerStr = int2chineseNum(Integer.parseInt(split[0]));

        sb.append(integerStr);

        //如果传入的数有小数，则进行切割，将整数与小数部分分离
        if (split.length == 2) {
            //有小数部分
            sb.append(CN_POINT);
            String decimalStr = split[1];
            char[] chars = decimalStr.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                int index = Integer.parseInt(String.valueOf(chars[i]));
                sb.append(CN_NUM[index]);
            }
        }

        //判断传入数字为正数还是负数
        int signum = bigDecimalNum.signum();
        if (signum == -1) {
            sb.insert(0, CN_NEGATIVE);
        }

        return sb.toString();
    }

}
